package com.finals.function.app;

import android.app.Activity;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;

import com.finals.function.FunctionActivity;
import com.finals.function.service.NotificationAccessibilityService;
import com.finals.function.util.NotificationUtil;
import com.finals.function.util.OnScreenCastCallback;
import com.finals.function.util.ScreenShotManager;
import com.finals.function.view.TouchBaseView;

public class BaseApplication extends Application {

	TouchBaseView mTouchView;

	ScreenShotManager mScreenShotManager;

	NotificationAccessibilityService mAccessibilityService;

	NotificationUtil mNotificationUtil;

	BaseConfig mBaseConfig;

	@Override
	public void onCreate() {
		super.onCreate();
		InitCreash();
		InitData();
	}

	private void InitData() {
		mNotificationUtil = new NotificationUtil(this);
		mTouchView = new TouchBaseView(this);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		mTouchView.setLayoutParams(params);
	}

	private void InitCreash() {
		CrashHandler mCrashHandler = new CrashHandler();
		mCrashHandler.init(this);
	}

	public void ShowTouch() {
		if (android.os.Build.VERSION.SDK_INT >= 20) {
			mScreenShotManager = new ScreenShotManager(this);
		}
		mTouchView.show();
	}

	public void HideTouch() {
		if (mTouchView != null) {
			mTouchView.dismiss();
		}
	}

	public void setAccessibilityService(NotificationAccessibilityService service) {
		this.mAccessibilityService = service;
		mTouchView.setAccessibilityService(service);
	}

	@Override
	public void onTerminate() {
		if (mScreenShotManager != null) {
			mScreenShotManager.onDestory();
		}
		if (mNotificationUtil != null) {
			mNotificationUtil.onDestory();
		}
		super.onTerminate();
	}

	public void startScreenShot(Activity mainActivity, int requestCode) {
		if (mScreenShotManager != null) {
			mScreenShotManager.startScreenShot(mainActivity, requestCode);
		}
	}

	public void InitMediaProjection(Intent data) {
		if (mScreenShotManager != null) {
			mScreenShotManager.InitMediaProjection(data);
		}
	}

	public boolean isSupportScreenShot() {
		if (mScreenShotManager == null) {
			return false;
		}
		if (!mScreenShotManager.isInit()) {
			return false;
		}
		return true;
	}

	public boolean isAccessibilityServiceRun() {
		if (mAccessibilityService != null) {
			return true;
		}
		return false;
	}

	public void StartShotCast(OnScreenCastCallback mCallback) {
		if (mScreenShotManager != null) {
			mScreenShotManager.setScreenCastCallback(mCallback);
			mScreenShotManager.StartShotCast();
		}
	}

	public void showNotification(String title, String content, PendingIntent mIntent, int id) {
		if (mNotificationUtil != null) {
			mNotificationUtil.showNotification(title, content, mIntent, id);
		}
	}

	/**
	 * 
	 * @param value
	 *            1表示下一首 <br>
	 *            2表示上一首 <br>
	 */
	public void ChangeSone(int value) {
		Intent intent = new Intent(this, FunctionActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra("Type", value);
		startActivity(intent);
	}

	/**
	 * 获取基本配置项
	 * 
	 * @return
	 */
	public BaseConfig getBaseConfig() {
		if (mBaseConfig == null) {
			mBaseConfig = new BaseConfig(this);
		}
		return mBaseConfig;
	}

	public void UpdateWindow() {
		if (mTouchView != null) {
			mTouchView.UpdateMode();
		} else {
			android.util.Log.e("Finals", "TouchView 为空");
		}
	}
}
